/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2022. All rights reserved.
 *
 */

import { ClassTypeConst } from '../../../common/options/class-type-const.js';
import { Module, Project } from '../../../external/core/hvigor-core-node.js';
import { Config, CoreConfig } from '../../../vigor/plugin/interface/loader-profile.js';

import { DefaultNodeImpl } from './default-node-impl.js';

/**
 * hvigor工程的基础root module
 *
 * @since 2021/12/27
 */
export class ProjectImpl extends DefaultNodeImpl implements Project {
  classKind = ClassTypeConst.HVIGOR_PROJECT;
  private readonly _subProjects: Map<string, Module>;
  private _config: any | undefined;

  constructor(projectName: string, moduleDir: string) {
    super(projectName, moduleDir);
    this._subProjects = new Map<string, Module>();
  }

  findModuleByName(moduleName: string): Module | undefined {
    return this._subProjects.get(moduleName);
  }

  getProject(): Project {
    return this;
  }

  getSubModules(): Map<string, Module> {
    return this._subProjects;
  }

  addSubModule(module: Module): void {
    this._subProjects.set(module.getName(), module);
  }

  getAllSubModules(): Module[] {
    const modules = [];
    for (const subModule of this._subProjects.values()) {
      modules.push(subModule);
    }
    return modules;
  }

  loadConfig(config: any) {
    this._config = config;
  }

  getConfigOpt(): Config {
    return new CoreConfig(this._config);
  }
}
